package controller

import (
	"github.com/gin-gonic/gin"
	"runtime"
	"time"
)

// 返回请求体
type ResponseData struct {
	Code    int         `json:"code"`
	Message string      `json:"message"`
	Data    interface{} `json:"data"`
}

type List struct {
	List  interface{} `json:"list"`
	Count int         `json:"count"`
}

// 默认的请求常量
const (
	SuccessMsg = "请求成功"
	ErrorMsg   = "请求失败"
)

// 请求成功返回的参数
func Success(context *gin.Context, data interface{}, message string) {
	if message == "" {
		message = SuccessMsg
	}
	resp := ResponseData{
		Code:    0,
		Message: message,
		Data:    data,
	}
	context.JSON(200, resp)
}

// 请求失败返回的参数
func Failed(context *gin.Context, message string, code int) {
	if message == "" {
		message = ErrorMsg
	}
	resp := ResponseData{
		Code:    code,
		Message: message,
		Data:    nil,
	}
	context.JSON(200, resp)
}

func GetLine() int {
	_, _, line, _ := runtime.Caller(1)
	return line
}

func ThisTime() string {
	return time.Now().Format("2006-01-02 15:04:05")
}
